<?php 
namespace admin\controllers;

use yii\web\Controller;
use yii\filters\AccessControl;
use common\helpers\ShowJson;
use common\helpers\ErrorHelper;
use common\models\bx\BxInfo;
use yii\data\Pagination;

class BxController extends Controller
{
    public $layout = "main2";
    
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['list', 'add', 'addsave', 'edit', 'editsave', 'delete', 'auditpass', 'settop'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }
    
    
    public function actionList()
    {
        $request = \Yii::$app->request;
        $pageSize = $request->get('page_size', 50);
        
        $where = array();
        $bxObj = BxInfo::find();
        $count = $bxObj->where($where)->count();
        $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $pageSize]);
        $bxList = $bxObj->where($where)->orderBy( 'id desc' )->offset( $pagination->offset )->limit( $pagination->limit )->asArray()->all();
        
        $data = array(
            'bxList' => $bxList,
        );
        
        return $this->render("list", $data);
    }
    
    // 添加
    public function actionAdd()
    {
        return $this->render('add');
    }
    
    public function actionAddsave()
    {
        $paramsError = ErrorHelper::get('paramsError');
        $request = \Yii::$app->request;
        $title = $request->post('title', '');
        $content = $request->post('content', '');
        $cover = $request->post('cover', '');
        $link = $request->post('link', '');
        if (empty($title) || empty($content) || empty($cover) || empty($link)) {
            ShowJson::showErrorJson($paramsError);
        }
        $nowTime = time();
        
        $bxInfoObj = new BxInfo();
        $bxInfoObj->title = $title;
        $bxInfoObj->content = $content;
        $bxInfoObj->cover = $cover;
        $bxInfoObj->link = $link;
        $bxInfoObj->status = 1;
        $bxInfoObj->updated_at = $nowTime;
        $bxInfoObj->created_at = $nowTime;
        $bxInfoObj->save();
        
        ShowJson::showSuccessJson();
    }
    
    // 编辑
    public function actionEdit()
    {
        return $this->render('edit');
    }
    
    public function actionEditsave()
    {
        $paramsError = ErrorHelper::get('paramsError');
        $request = \Yii::$app->request;
        $id = $request->post('id', 0);
        $title = $request->post('title', '');
        $content = $request->post('content', '');
        $cover = $request->post('cover', '');
        $link = $request->post('linke', '');
        if (empty($id)) {
            ShowJson::showErrorJson($paramsError);
        }
        
        $bxError = ErrorHelper::get('bxError');
        $bxObj = BxInfo::find()->where(['id' => $id]);
        if (empty($bxObj)) {
            ShowJson::showErrorJson($bxError);
        }
        if (!empty($title)) {
            $bxObj->title = $title;
        }
        if (!empty($content)) {
            $bxObj->content = $content;
        }
        if (!empty($cover)) {
            $bxObj->cover = $cover;
        }
        if (!empty($link)) {
            $bxObj->link = $link;
        }
        $bxObj->updated_at = time();
        $bxObj->save();
        
        ShowJson::showSuccessJson();
    }
    
    
    // 删除
    public function actionDelete()
    {
        $paramsError = ErrorHelper::get('paramsError');
        $request = \Yii::$app->request;
        $id = $request->get('id', 0);
        if (empty($id)) {
            ShowJson::showErrorJson($paramsError);
        }
        
        $bxError = ErrorHelper::get('bxError');
        $bxObj = BxInfo::find()->where(['id' => $id]);
        if (empty($bxObj)) {
            ShowJson::showErrorJson($bxError);
        }
        $bxObj->status = 0;
        $bxObj->updated_at = time();
        $bxObj->save();
        
        ShowJson::showSuccessJson();
    }
    
    
    // 审核通过
    public function actionAuditpass()
    {
        $paramsError = ErrorHelper::get('paramsError');
        $request = \Yii::$app->request;
        $id = $request->get('id', 0);
        if (empty($id)) {
            ShowJson::showErrorJson($paramsError);
        }
        
        $bxError = ErrorHelper::get('bxError');
        $bxObj = BxInfo::find()->where(['id' => $id]);
        if (empty($bxObj)) {
            ShowJson::showErrorJson($bxError);
        }
        $bxObj->status = 2;
        $bxObj->updated_at = time();
        $bxObj->save();
        
        ShowJson::showSuccessJson();
    }
    
    
    // 置顶-取消置顶
    public function actionSettop()
    {
        $paramsError = ErrorHelper::get('paramsError');
        $request = \Yii::$app->request;
        $id = $request->get('id', 0);
        $isTop = $request->get('is_top', 0);
        if (empty($id)) {
            ShowJson::showErrorJson($paramsError);
        }
        
        $bxError = ErrorHelper::get('bxError');
        $bxObj = BxInfo::find()->where(['id' => $id]);
        if (empty($bxObj)) {
            ShowJson::showErrorJson($bxError);
        }
        if ($isTop == 1) {
            $bxObj->is_top = 1;
        } elseif ($isTop == 0) {
            $bxObj->is_top = 0;
        }
        $bxObj->updated_at = time();
        $bxObj->save();
        
        ShowJson::showSuccessJson();
    }
}
?>